package org.example.elm_xiangmu.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.example.elm_xiangmu.po.Cart;
import org.example.elm_xiangmu.result.Result;
import org.example.elm_xiangmu.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/Cart")
@Tag(name="购物车")
public class CartController {
    @Autowired
    private CartService cartService;

    @GetMapping("/listCart")
    @Operation(summary = "查询购物车信息")
    public Result listCart(@Parameter(description = "用户编号") @RequestParam String userId,
                           @Parameter(description = "商家编号") @RequestParam (defaultValue="0") Integer businessId ){
        List<Cart> cartList=cartService.listCart(userId,businessId);
        if (cartList == null || cartList.size()==0){
            return  Result.error("该用户未在该商家购物");
        }
        return Result.success(cartList,"查询购物车信息成功");
    }

    /**
     * 返回更新的行数
     * @param userId
     * @param businessId
     * @param foodId
     * @return
     */
    @PostMapping("/saveCart")
    @Operation(summary = "添加购物车信息")
    public Result saveCart(@Parameter(description = "用户编号") @RequestParam String userId,
                           @Parameter(description = "商家编号") @RequestParam Integer businessId,
                           @Parameter(description = "食品编号") @RequestParam Integer foodId) {
        Cart cart=new Cart();
        cart.setUserId(userId);
        cart.setBusinessId(businessId);
        cart.setFoodId(foodId);
        //TODO 禁止重复添加
        //添加时默认数量为1
        cart.setQuantity(1);
        cartService.saveCart(cart);
        //TODO 获取影响的行数 主键自增
        return Result.success();
    }

    /**
     * 返回更新的行数
     * @param userId
     * @param businessId
     * @param foodId
     * @return
     */
    @PutMapping("/updateCart")
    @Operation(summary = "更新购物车信息")
    public Result updateCart(@Parameter(description = "用户编号") @RequestParam String userId,
                             @Parameter(description = "商家编号") @RequestParam Integer businessId,
                             @Parameter(description = "食品编号") @RequestParam Integer foodId,
                             @Parameter(description = "新增数量") @RequestParam Integer quantity){
        Cart cart=new Cart();
        cart.setUserId(userId);
        cart.setBusinessId(businessId);
        cart.setFoodId(foodId);
        cart.setQuantity(quantity);
        if(quantity >= 0 ){
            cartService.updateCart(cart);
            return Result.success("修改成功");
        }else {
            return Result.error("数量有错误");
        }

    }
//
@Operation(summary = "删除食品记录")
@DeleteMapping("/removeCart")
public Result removeCart(@Parameter(description = "用户编号") @RequestParam String userId,
                         @Parameter(description = "商家编号") @RequestParam Integer businessId,
                         @Parameter(description = "食品编号") @RequestParam (defaultValue="0") Integer foodId){
    Cart cart=new Cart();
    cart.setUserId(userId);
    cart.setBusinessId(businessId);
    cart.setFoodId(foodId);
    if(cartService.remove(cart)){
        return Result.success();
    }else{
        return Result.error("删除失败！");
    }
}



}
